package com.roc.steelend.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.roc.steelend.controller.vo.returnformat.ReturnMsgForTable;
import com.roc.steelend.service.EmployeeService;
import com.roc.steelend.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@Api(tags = "员工功能接口")
@CrossOrigin
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/v1/show/employee/detail")
    public ReturnMsgForTable showEmployeeDetail(@RequestParam(value = "now",required = true)Integer now,
                                                @RequestParam(value = "size",required = true)Integer size,
                                                @RequestParam(value = "departmentId",required = false)Integer departmentId){


     return    employeeService.findEmployeeByShow(now,size,departmentId);
    }



    @GetMapping("/v1/find/employee/data")
    public List<HashMap<String,Object>> findEmployeeData(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();

        return employeeService.findEmployeeData(Integer.valueOf(employeeIdAsString));
    }

    @GetMapping("/v1/find/employee/msg")
    @ApiOperation(value = "返回用户的具体信息(给用户个人信息界面使用)")
    public List<HashMap<String,Object>> findEmployeeMsg(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();

        return employeeService.findEmployeeMsg(Integer.valueOf(employeeIdAsString));
    }




}
